package com.dianrui.qiyouriding.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.adapter.FeedBackInfoAdapter;
import com.dianrui.qiyouriding.bean.Datas;
import com.dianrui.qiyouriding.util.GlideUtil;
import com.dianrui.qiyouriding.okhttp.OkHttpRequest;
import com.dianrui.qiyouriding.util.StringUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.dianrui.qiyouriding.util.Url;
import com.dianrui.qiyouriding.view.ninegride.NineView;
import com.dianrui.qiyouriding.view.ninegride.RatioImageView;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FeedBackInfoActivity extends BaseActivity implements NineView.NineViewShowCallBack{
    private List<String> imageList = new ArrayList<>();
    private List<Datas> feedList = new ArrayList<>();
    private FeedBackInfoAdapter feedBackInfoAdapter;
    private int feedbackIds;
    private boolean hasChecked = false;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.nine)
    NineView nine;
    @BindView(R.id.input)
    EditText input;
    @BindView(R.id.reply)
    EditText replys;
    @BindView(R.id.grid)
    GridView feedGrid;


    @Override
    public int getContentViewId() {
        return R.layout.activity_feedbackinfo;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle bundle=getIntent().getExtras();
            if (bundle != null) {
                feedbackIds = bundle.getInt("feedbackId");
            }
        }
        customInit(true,R.color.main_color);
        title.setText(getString(R.string.feedback_info));
        nine.maxCount(9).spacing(3).setNineViewShowCallBack(this).setUrlList(imageList);
        feedBackInfoAdapter=new FeedBackInfoAdapter(FeedBackInfoActivity.this,feedList);
        feedGrid.setAdapter(feedBackInfoAdapter);
        getFeedBackTypeInfo();
    }

    @Override
    public void initData() {

    }

    private void getFeedBackTypeInfo() {
        JsonObject getfeed=new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            getfeed.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        getfeed.addProperty("feedback_id", feedbackIds);
        OkHttpRequest.getInstance().postRequests(Url.FEED_BACK_INFO, getfeed.toString(),  new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String response) {
                if (!StringUtils.isEmpty(response)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(response);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                if (StringUtils.isEmpty(jsonObject.optJSONObject("data").optString("reply"))) {
                                    replys.setVisibility(View.GONE);
                                } else {
                                    replys.setVisibility(View.VISIBLE);
                                    replys.setText(jsonObject.optJSONObject("data").optString("reply"));
                                }

                                if (StringUtils.isEmpty(jsonObject.optJSONObject("data").optString("content"))) {
                                    input.setVisibility(View.GONE);
                                } else {
                                    input.setText(jsonObject.optJSONObject("data").optString("content"));
                                    input.setMovementMethod(ScrollingMovementMethod.getInstance());
                                    input.setVisibility(View.VISIBLE);
                                }

                                JSONArray jsonArray = jsonObject.optJSONObject("data").optJSONArray("photo");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        String str = jsonArray.optString(i);
                                        imageList.add(str);
                                    }
                                    nine.urlList(imageList);
                                    nine.setVisibility(View.VISIBLE);
                                } else {
                                    nine.setVisibility(View.GONE);
                                }
                                JSONArray jsonArray1 = jsonObject.optJSONObject("data").optJSONArray("feedback_list");
                                if (jsonArray1.length() > 0) {
                                    for (int i = 0; i < jsonArray1.length(); i++) {
                                        Datas feeddatas = new Datas();
                                        feeddatas.form_id = jsonArray1.optJSONObject(i).optInt("form_id");
                                        feeddatas.name = jsonArray1.optJSONObject(i).optString("name");
                                        feeddatas.isChecked = jsonArray1.optJSONObject(i).optInt("is_check");
                                        feedList.add(feeddatas);
                                    }
                                    if (feedBackInfoAdapter != null) {
                                        feedBackInfoAdapter.refresh(feedList);
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }
    @Override
    public void onDisplayOneImage(RatioImageView imageView, String url, int parentWidth, Context context) {
        if ("default".equals(url)) {
        } else {
            imageView.setTag(R.id.glide_tag, url);
            addImageViewList(GlideUtil.downLoader(false, imageView, FeedBackInfoActivity.this));
        }
    }

    @Override
    public void onDisplayImage(RatioImageView imageView, RatioImageView closeButton, String url, int parentWidth, Context context) {
        if ("default".equals(url)) {
        } else {
            imageView.setTag(R.id.glide_tag, url);
            addImageViewList(GlideUtil.downLoader(false, imageView, FeedBackInfoActivity.this));
        }
    }

    @Override
    public void onClickImage(int position, String url, List<String> urlList, Context context) {
        List<String> temp = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < imageList.size(); i++) {
            if (!"default".equals(imageList.get(i))) {
                temp.add(imageList.get(i));
            } else {
                count++;
            }
        }
        startActivity(new Intent(FeedBackInfoActivity.this, BigImageActivity.class)
                .putStringArrayListExtra("data", (ArrayList<String>) temp)
                .putExtra("current", position));
    }

    @Override
    public void onClickImageColse(int position, String url, List<String> urlList, Context context) {

    }

    @Override
    public void onSamePhotos(List<String> mRejectUrlList) {

    }

    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }
}
